function [ym,xm] = mk_vdm(datamatrix,nlags,c)
% PURPOSE: generates data matrices needed for VAR estimation 
% -----------------------------------------------------
% USAGE: [ym,xm] = mk_vdm(ndxv,adm)
% where: 
% nlags = lag length of VAR model  
% datamatrix  = (nobs x nvar) data matrix = [Y]  
% c=0: neither constant nor trend; c=1: constant; c=2: costant and trend; c=3: costant, trend, and trend^2.
% NOTE: no dimension checks imposed
% -----------------------------------------------------
% RETURNS: 
% ym = (nobs x nvar) matrix with obs on endog variables
% xm = (nobs x nkk)  matrix with regressors
% nkk= nvar*nlags
% -----------------------------------------------------
% written by:
% Andrea Tamoni
% andrea.tamoni@phd.unibocconi.it
% � Dec 2007

if nargin==2, 
    c=1; 
end;
nvar =size(datamatrix,2);
ntall=size(datamatrix,1); %Total number of observation
nt=ntall-nlags;           %Effective sample
nkk=nvar*nlags;
xm=zeros(nt,nkk);
ym=datamatrix(nlags+1:ntall,1:nvar);%Adjusting Y so that dimension of Y and X match
for il=1:nlags
    xm(:,((il-1)*nvar+1):(il*nvar))=datamatrix((nlags-il+1):(ntall-il),1:nvar);
end

if c==1, 
    xm=[ones(nt,1)              xm];
elseif c==2,
    xm=[ones(nt,1) (1:nt)'      xm];
elseif c==3,
    xm=[ones(nt,1) ((1:nt).^2)' xm];
end